﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;

namespace TouchUmlUI
{

    [Serializable]
    public class MRU
    {        
        String[] list;
        private int size;

        public void Debug()
        {
            for (int i = 0; i < list.Length; i++)
            {
                System.Console.WriteLine(i + ". " + list[i]);
            }
        }

        public void Save(string fileName)
        {
            Stream stream = null;
            try
            {
                IFormatter formatter = new BinaryFormatter();
                stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);                
                formatter.Serialize(stream, this);
            }
            catch
            {
                // do nothing, just ignore any possible errors
            }
            finally
            {
                if (null != stream)
                    stream.Close();
            }
        }

        public static MRU Load(string fileName)
        {
            Stream stream = null;
            MRU mruList = null;
            try
            {
                IFormatter formatter = new BinaryFormatter();
                stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None);
                mruList = (MRU)formatter.Deserialize(stream);                                
            }
            catch
            {
                // do nothing, just ignore any possible errors
            }
            finally
            {
                if (null != stream)
                    stream.Close();
            }
            return mruList;
        }

        public MRU(int size)
        {
            this.size = size;
            list = new String[size];
            Array.Clear(list, 0, size);
        }

        public String[] getList()
        {
            return list;
        }

        public void addToList(String fileName)
        {
            int foundId = -1;
            for (int i = 0;i<list.Length; i++)
            {
                if (fileName.CompareTo(list[i])==0)
                {
                    foundId = i;
                    break;
                }
            }
            //System.Console.WriteLine("foundId: " + foundId);
            // jezeli znaleziono na liscie to przesun na pierwsze miejsce 
            if (foundId!=-1)
            {
                for (int i = foundId; i >= 1; i--)
                {
                    list[i] = list[i - 1];
                }
                list[0] = fileName;
            } // jezeli nie znaleziono to przesun wszystkie na dol i dodaj na pierwsze miejsce nowy
            else
            {
                for (int i = list.Length - 1; i >= 1; i--)
                {                    
                    list[i] = list[i - 1];
                }
                list[0] = fileName;
            }
        }

    }
}
